local Config = require("app.views.slots.Config")
local SymbolCfg = Config.SymbolCfg
local SymbolNode = class("SymbolNode", cc.Node)
local cellSize = Config.CellSize

function SymbolNode:ctor(tid)
    self._tid = tid
    local cfg = SymbolCfg[tid]
    self._cfg = cfg
    self._size = cc.size(cellSize.width*cfg.size[1], cellSize.height*cfg.size[2])

    self._state = "idle"

    local drawNode = cc.DrawNode:create()
    self:addChild(drawNode, 1)
    drawNode:drawRect(cc.p(0, 0), cc.p(cellSize.width*cfg.size[1], -cellSize.height*cfg.size[2]), cc.c4f(1, 1, 1, 1))

    self:_init()
end

function SymbolNode:_init()
    self:setCascadeOpacityEnabled(true)
    self:_initBgRes()
    self:_initMask()
end

function SymbolNode:_initBgRes()
    local bgResName = self._cfg.bg
    local bg = display.newSprite(string.format("slots/%s.png", bgResName))
    self:addChild(bg)
    bg:setAnchorPoint(0, 1)
    bg:setPosition(0, 0)
end

function SymbolNode:_initMask()
    local mask = display.newSprite("slots/white_bg.png")
    mask:setOpacity(0)
    mask:setContentSize(self._size)
    mask:setAnchorPoint(0, 1)
    mask:setColor(cc.c3b(0, 0, 0))
    self:addChild(mask, 999)
    mask:setPosition(0, 0)
    self._mask = mask
end

-- 获取像素高度
function SymbolNode:getSize()
    return self._size
end

function SymbolNode:getTid()
    return self._tid
end

function SymbolNode:playClearEffect()
    self._isFake = true
    self:runAction(cc.Sequence:create(
        cc.DelayTime:create(Config.Time_MaskFadeIn + 1),
        cc.FadeTo:create(Config.Time_MaskFadeOut, 0),
        cc.CallFunc:create(function()
            self:setVisible(false)
        end)
    ))
end

function SymbolNode:enableMask(bool)
    self._mask:stopAllActions()
    self._mask:setOpacity(0)
    self._mask:runAction(cc.Sequence:create(
        cc.FadeTo:create(Config.Time_MaskFadeIn, Config.Alpha_Mask),
        cc.DelayTime:create(1),
        cc.FadeTo:create(Config.Time_MaskFadeOut, 0)
    ))
end

-- 模糊效果
function SymbolNode:enableBlur(bool)
    -- TODO
end

function SymbolNode:setFake(bool)
    self._isFake = bool
end

function SymbolNode:isFake()
    return self._isFake
end

function SymbolNode:update(dt)
    if self._state == "dropping" then
        local selfYPos = self:getPositionY()
        if selfYPos <= self._targetYPos then
            self:setPositionY(self._targetYPos)
            self._speed = 100
            self._state = "bouncing"
        else
            self._speed = self._speed - dt * 1000
            self:setPositionY(selfYPos + self._speed * dt)
        end
    elseif self._state == "bouncing" then
        local selfYPos = self:getPositionY()
        if selfYPos < self._targetYPos then
            self:setPositionY(self._targetYPos)
            self._speed = 100
            self._state = "idle"
        else
            self._speed = self._speed - dt * 1000
            self:setPositionY(selfYPos + self._speed * dt)
        end
    end
end

-- 填充下落的目标位置
function SymbolNode:setStartDrop(y)
    if math.floor(math.abs(self:getPositionY() - y)) == 0 then
        return
    end
    self._state = "dropping"
    self._targetYPos = y
    self._speed = -500
end

return SymbolNode